<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery.js"></script>
    <style>
      table {
            /*用于让表格边框合并为一条实线 */
            border-collapse: collapse;
            margin-top: 20px;
            width: 400px;
        }

        table,
        th,
        td {
            /* 定义单元格边框的样式，这里设置为1像素的黑色实线 */
            border: 1px solid rgb(34, 10, 245);
            text-align: center;
            padding: 5px;
        }
    </style>
</head>
<body>
    <button>创建数据</button>
    <button>添加数据</button>
    <button>删除数据</button>
    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>地址</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <script>
        $(function(){
            let data=[
                {
                    name:"丽丽",
                    age:18,
                    sex:"女",
                    address:"北京"
                },
                {
                    name:"张三",
                    age:18,
                    sex:"男",
                    address:"北京"
                }
            ]
            //创建数据
            
            
            $("button").eq(0).click(function(){
                 let str = "";
                        data.forEach(function(val){
                               str += `<tr>
                                        <td>${val.name}</td>
                                        <td>${val.age}</td>
                                        <td>${val.sex}</td>
                                        <td>${val.address}</td>
                                        <td><button>删除</button></td>
                                    </tr>`
                        })
                       
                     
                        $("tbody").html(str);
            })
           

            //添加数据
            $("button").eq(1).click(function(){
                data.push({
                    name:"李四",
                    age:18,
                    sex:"男",
                    address:"北京"
                })
                let str = "";
                        data.forEach(function(val){
                               str += `<tr>
                                        <td>${val.name}</td>
                                        <td>${val.age}</td>
                                        <td>${val.sex}</td>
                                        <td>${val.address}</td>
                                        <td><button>删除</button></td>
                                    </tr>`
                        })
                       
                     
                        $("tbody").html(str);
            })

            //删除数据
            $("button").eq(2).click(function(){
                data.pop();
                let str = "";
                        data.forEach(function(val){
                               str += `<tr>
                                        <td>${val.name}</td>
                                        <td>${val.age}</td>
                                        <td>${val.sex}</td>
                                        <td>${val.address}</td>
                                        <td><button>删除</button></td>
                                    </tr>`
                        })
                       
                     
                        $("tbody").html(str);
            })
        })
    </script>
</body>
</html>

<!-- 
 $.ajax({
                    type:"get",
                    url:"http://localhost:8080/data",
                    dataType:"json",
                    success:function(res){
                        console.log(res.obj);
                        let str = "";
                        res.obj.forEach(function(val){
                               str += `<tr>
                                        <td>${val.name}</td>
                                        <td>${val.age}</td>
                                        <td>${val.sex}</td>
                                        <td>${val.address}</td>
                                        <td><button>删除</button></td>
                                    </tr>`
                        })
                       
                     
                        $("tbody").html(str);
                    }
                }) -->